//
//  AnimationViewController.m
//  TotalDemo
//
//  Created by gujian on 16/3/17.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "AnimationViewController.h"
#import "LBAnimation.h"

static const CGFloat VIEW_WIDTH = 70.0;
static const NSUInteger VIEW_COUNT = 3;
static const CGFloat ANIMATION_DURATION = 1.0;
static const CGFloat ANIMATIN_MIN_DURATION = 0.05;

@interface AnimationViewController()

@property (nonatomic, strong) UIImageView *throwImageView;

@property (nonatomic, strong) UIView *showAnimationView;

@property (nonatomic, strong) UIView *firstView;

@property (nonatomic, strong) UIView *secView;

@property (nonatomic, strong) NSArray<UIView *> *viewArr;


@end

@implementation AnimationViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"动画";

    [self throwAnimation];
    [self showAnimation];
    
}

- (void)throwAnimation
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [imageView setImage:[UIImage imageNamed:@"animation_plane"]];
    [self.view addSubview:imageView];
    _throwImageView = imageView;
    
    UIButton *throwBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [throwBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [throwBtn setTitle:@"移动" forState:UIControlStateNormal];
    [throwBtn addTarget:self action:@selector(throwAnimationAction:) forControlEvents:UIControlEventTouchUpInside];
    throwBtn.frame = CGRectMake(imageView.right + 80, 0, 50, 50);
    [self.view addSubview:throwBtn];


}

- (void)throwAnimationAction:(UIButton *)btn
{
    [[LBAnimation sharedInstance]throwObject: _throwImageView From:_throwImageView.center To:CGPointMake(SCREEN_WIDTH/2.0, SCREEN_HEIGHT) Scale:0.2];
}

#pragma mark - 菜单动画
- (void)showAnimation
{
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"菜单" style:UIBarButtonItemStylePlain target:self action:@selector(showAnimationAction:)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
    _showAnimationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _showAnimationView.backgroundColor = [UIColor whiteColor];
    _showAnimationView.alpha = .1;//如果不设置为，则动画实际执行时间始终为0
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAnimationAction:)];
    [_showAnimationView addGestureRecognizer:tapGesture];
    

    NSInteger count = VIEW_COUNT;
    
    NSMutableArray *tempArr = [NSMutableArray arrayWithCapacity:count];
    
    CGFloat marginRight = 30.0;
    CGFloat paddingRight = (SCREEN_WIDTH - 2 * marginRight - VIEW_COUNT * VIEW_WIDTH)/(VIEW_COUNT-1);
    
    CGFloat offX = marginRight;
    for (int i = 0; i < 3 * VIEW_COUNT; i++) {
        offX = marginRight + (i%VIEW_COUNT) * (paddingRight + VIEW_WIDTH);
        UIView *view = [self createSubView];
        view.frame = CGRectMake(offX, SCREEN_HEIGHT , VIEW_WIDTH, VIEW_WIDTH);
        view.layer.cornerRadius = 30;
        view.layer.masksToBounds = YES;
        [tempArr addObject:view];
    }
    _viewArr = [tempArr copy];
    

}

- (UIView *)createSubView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, VIEW_WIDTH, VIEW_WIDTH)];
    view.backgroundColor = [UIColor randomColor];
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:view];
    return view;
}

- (void)showAnimationAction:(UIButton *)btn
{
    
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [window insertSubview:_showAnimationView atIndex:window.subviews.count-3*VIEW_COUNT];
        _showAnimationView.alpha = 1.0;
        
        for (int i = 0; i < 3 * VIEW_COUNT; i++) {
            UIView *view = [_viewArr objectAtIndex:i];
            NSUInteger multi = i / VIEW_COUNT;
            CGFloat offY = 150 + multi * 1.5 * VIEW_WIDTH;
            [view.layer addAnimation: [[LBAnimation sharedInstance] keyAnimationObject:view Pos:CGPointMake(view.centerX, offY) TimeOffset:ANIMATIN_MIN_DURATION * i] forKey:nil];
        }
        
    } completion:^(BOOL finished) {
    
//        if (finished) {
//            for (UIView *view in _viewArr) {
//                [view.layer removeAllAnimations];
//            }
//        }
    }];
    
}


- (void)hideAnimationAction:(UITapGestureRecognizer *)gesture
{
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        _showAnimationView.alpha = 0.1;
        for (NSInteger i = _viewArr.count-1; i >= 0; i--) {
            UIView *view = [_viewArr objectAtIndex:i];
            [view.layer addAnimation:[[LBAnimation sharedInstance] animationWithObject:view Pos:CGPointMake(view.centerX, SCREEN_HEIGHT+VIEW_WIDTH/2.0) TimeOffset:ANIMATIN_MIN_DURATION * (_viewArr.count-1-i)]forKey:nil];
        }
        
    } completion:^(BOOL finished) {
        if (finished) {
            [_showAnimationView removeFromSuperview];
//            for (UIView *view in _viewArr) {
//                [view.layer removeAllAnimations];
//            }
        }

    }];
}


#pragma mark - 旋转木马动画
- (void)showCarouselAnimation
{
    
}


@end
